package org.jeecg.modules.zhoupackage.GoodsOrder.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.zhouAdmin.admin.goodsCenter.mapper.*;
import org.jeecg.modules.zhouAdmin.admin.goodsCenter.pojo.*;
import org.jeecg.modules.zhoupackage.GoodsOrder.mapper.MyMlccThirdCartBuynumMapper;
import org.jeecg.modules.zhoupackage.GoodsOrder.mapper.MyMlccThirdGoodsCartMapper;
import org.jeecg.modules.zhoupackage.GoodsOrder.pojo.MlccThirdCartBuynum;
import org.jeecg.modules.zhoupackage.GoodsOrder.pojo.MlccThirdGoodsCart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * 现货下单工具类
 *
 * @author zxl
 * @since 2020-04-01
 */
@Component
public class GoodsOrderUtil {

    @Autowired
    MyMlccThirdGoodsRangeMapper rangeMapper;

    @Autowired
    MyMlccThirdCartBuynumMapper cartBuynumMapper;

    @Autowired
    MyMlccThirdGoodsSpecificationsMapper goodsSpecificationsMapper;

    @Autowired
    MyMlccThirdGoodsSizeMapper goodsSizeMapper;

    @Autowired
    MyMlccThirdGoodsMapper goodsMapper;

    @Autowired
    MyMlccThirdGoodsCartMapper cartMapper;

    @Autowired
    MyMlccThirdGoodsSeriesMapper goodsSeriesMapper;

    /**
     * 根据购买数量获取阶梯价
     *
     * @param groupid 商品系列id
     * @param number  购买数量
     */
    public MlccThirdGoodsRange getGoodsRange(String groupid, String number) {
        QueryWrapper<MlccThirdGoodsRange> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_series_id", groupid).le("min", number).gt("max", number);
        return rangeMapper.selectOne(wrapper);
    }

    /**
     * 查询购物车下每个产品不同规格的数量
     *
     * @param cartid 购物车id
     */
    public List<MlccThirdCartBuynum> getCartByNum(String cartid) {
        QueryWrapper<MlccThirdCartBuynum> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_cart_id", cartid).eq("status", 0);
        List<MlccThirdCartBuynum> buynums = cartBuynumMapper.selectList(wrapper);
        if (buynums != null && buynums.size() > 0) {
            for (MlccThirdCartBuynum buynum : buynums) {
                // 给每个不同规格查询规格名称和尺寸信息
                MlccThirdGoodsSpecifications specifications = goodsSpecificationsMapper.selectGuigeById(buynum.getGoodsSpecificationsId());
                MlccThirdGoodsSize goodsSize = goodsSizeMapper.selectById(buynum.getGoodsSizeId());
                buynum.setGoodsSpecifications(specifications).setGoodsSize(goodsSize);
            }
        }
        return buynums;
    }

    /**
     * 更新购物车金额
     *
     * @param cartid  购物车id
     * @param goodsid 商品id
     * @param num     购买数量
     */
    public int updateCartPrice(String cartid, String goodsid, String num) {
        MlccThirdGoods goods = goodsMapper.selectById(goodsid);
        MlccThirdGoodsCart goodsCart = cartMapper.selectById(cartid);
        if (goods != null && goodsCart != null) {
            if (goods.getIsMain() == 0) {
                MlccThirdGoodsRange goodsRange = getGoodsRange(goods.getGoodsSeriesId(), num);
                if (goodsRange != null) {
                    // 阶梯价
                    BigDecimal price = goodsRange.getPrice();
                    goodsCart.setGoodsNum(num);
                    // 计算价格
                    BigDecimal multiply = price.multiply(new BigDecimal(num));
                    goodsCart.setSumMoney(multiply.setScale(2, BigDecimal.ROUND_HALF_UP));
                    return cartMapper.updateById(goodsCart);
                } else {
                    // 说明没有设置阶梯价或者没有对应的阶梯价，使用默认价格
                    MlccThirdGoodsSeries goodsSeries = goodsSeriesMapper.selectById(goods.getGoodsSeriesId());
                    if (goodsSeries != null) {
                        // 拿到单价
                        BigDecimal price = goodsSeries.getRetailPrice();
                        BigDecimal multiply = price.multiply(new BigDecimal(num));
                        goodsCart.setSumMoney(multiply.setScale(2, BigDecimal.ROUND_HALF_UP));
                        return cartMapper.updateById(goodsCart);
                    }
                }
            } else {
                // 主推商品
                BigDecimal price = goods.getPrice();
                BigDecimal multiply = price.multiply(new BigDecimal(num));
                goodsCart.setSumMoney(multiply.setScale(2, BigDecimal.ROUND_HALF_UP));
                return cartMapper.updateById(goodsCart);
            }
        }
        return 0;
    }
}
